/**
 * 微信公众平台开发模式(JAVA) SDK
 * (c) 2012-2013 ____′↘夏悸 <wmails@126.cn>, MIT Licensed
 * http://www.jeasyuicn.com/wechat
 */
package com.weixin.sdk.kit;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;

import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Response;


/**
 * https 请求 微信为https的请求
 *
 * @author L.cm
 * @date 2013-10-9 下午2:40:19
 */ 
public class HttpKit {
	private static final String DEFAULT_CHARSET = "UTF-8";
    /**
     * @return 返回类型:
     * @throws IOException
     * @throws UnsupportedEncodingException
     * @throws NoSuchProviderException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     * @description 功能描述: get 请求
     */
    public static String get(String url, Map<String, String> params, Map<String, String> headers){
        try {
			AsyncHttpClient http = new AsyncHttpClient();
			AsyncHttpClient.BoundRequestBuilder builder = http.prepareGet(url);
			builder.setBodyEncoding(DEFAULT_CHARSET);
			if (params != null && !params.isEmpty()) {
			    Set<String> keys = params.keySet();
			    for (String key : keys) {
			        builder.addQueryParameter(key, params.get(key));
			    }
			}

			if (headers != null && !headers.isEmpty()) {
			    Set<String> keys = headers.keySet();
			    for (String key : keys) {
			        builder.addHeader(key, params.get(key));
			    }
			}
			Future<Response> f = builder.execute();
			String body = f.get().getResponseBody(DEFAULT_CHARSET);
			http.close();
			return body;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return url;
    }

    /**
     * @return 返回类型:
     * @throws IOException
     * @throws UnsupportedEncodingException
     * @throws NoSuchProviderException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     * @description 功能描述: get 请求
     */
    public static String get(String url) {
        return get(url, null);
    }

    /**
     * @return 返回类型:
     * @throws IOException
     * @throws NoSuchProviderException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     * @throws UnsupportedEncodingException
     * @description 功能描述: get 请求
     */
    public static String get(String url, Map<String, String> params)  {
        return get(url, params, null);
    }

    /**
     * @return 返回类型:
     * @throws IOException
     * @throws NoSuchProviderException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     * @description 功能描述: POST 请求
     */
    public static String post(String url, Map<String, String> params){
        try {
			AsyncHttpClient http = new AsyncHttpClient();
			AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(url);
			builder.setBodyEncoding(DEFAULT_CHARSET);
			if (params != null && !params.isEmpty()) {
			    Set<String> keys = params.keySet();
			    for (String key : keys) {
			        builder.addParameter(key, params.get(key));
			    }
			}
			Future<Response> f = builder.execute();
			String body = f.get().getResponseBody(DEFAULT_CHARSET);
			http.close();
			return body;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return url;
    }

    /**
     * 上传媒体文件
     *
     * @param url
     * @param file
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchProviderException
     * @throws KeyManagementException
     */
//    public static String upload(String url, File file) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException, ExecutionException, InterruptedException {
//        AsyncHttpClient http = new AsyncHttpClient();
//        AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(url);
//        builder.setBodyEncoding(DEFAULT_CHARSET);
//        String BOUNDARY = "----WebKitFormBoundaryiDGnV9zdZA1eM1yL"; // 定义数据分隔线
//        builder.setHeader("connection", "Keep-Alive");
//        builder.setHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36");
//        builder.setHeader("Charsert", "UTF-8");
//        builder.setHeader("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
//        byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线
//        builder.setBody(new UploadEntityWriter(end_data, file));
//
//        Future<Response> f = builder.execute();
//        String body = f.get().getResponseBody(DEFAULT_CHARSET);
//        http.close();
//        return body;
//    }

    /**
     * 下载资源
     *
     * @param url
     * @return
     * @throws IOException
     */
//    public static Attachment download(String url) throws ExecutionException, InterruptedException, IOException {
//        Attachment att = new Attachment();
//        AsyncHttpClient http = new AsyncHttpClient();
//        AsyncHttpClient.BoundRequestBuilder builder = http.prepareGet(url);
//        builder.setBodyEncoding(DEFAULT_CHARSET);
//        Future<Response> f = builder.execute();
//        if (f.get().getContentType().equalsIgnoreCase("text/plain")) {
//            att.setError(f.get().getResponseBody(DEFAULT_CHARSET));
//        } else {
//            BufferedInputStream bis = new BufferedInputStream(f.get().getResponseBodyAsStream());
//            String ds = f.get().getHeader("Content-disposition");
//            String fullName = ds.substring(ds.indexOf("filename=\"") + 10, ds.length() - 1);
//            String relName = fullName.substring(0, fullName.lastIndexOf("."));
//            String suffix = fullName.substring(relName.length() + 1);
//
//            att.setFullName(fullName);
//            att.setFileName(relName);
//            att.setSuffix(suffix);
//            att.setContentLength(f.get().getHeader("Content-Length"));
//            att.setContentType(f.get().getContentType());
//            att.setFileStream(bis);
//        }
//        http.close();
//        return att;
//    }

    public static String post(String url, String s) {
        try {
			AsyncHttpClient http = new AsyncHttpClient();
			AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(url);
			builder.setBodyEncoding(DEFAULT_CHARSET);
			builder.setBody(s);
			Future<Response> f = builder.execute();
			String body = f.get().getResponseBody(DEFAULT_CHARSET);
			http.close();
			return body;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return s; 
    }
    
//    public  static void main(String[] args) throws Exception{
//    	
//    	String accessToken = "ulhEL9F2CciJezmGj47C-d3hAJZwXiAANctVIwSHwBRK7Z1enIRWeZKZekk8jS5abIkCo2YmMSDlqUFKOKvSaw";
//    	String openId = "oeZTVt6XlCphRnCI-DlpdTyk27p4";
//
//    	//accessToken=WeChat.getAccessToken();
//    	accessToken="OmNg7HWCLNf8_ErhFYEpBaY7NVC_-VxL83dKD_RDnC41F5C9YEGmWFGsy3_RaOerPAieagVrdtkvEaUAg2gL6yw_-AtpMPXvivRO3pMYjoI";
//    	
//    	System.out.println(accessToken);
//    	
//    	openId="olcaSt3jokvXtL5Vdyzd-XU2W07I";
//    	//openId="olcaSt3wzZYpNC6pjLQh3YtGuK4U";
//    	
//		UserInfo u = WeChat.user.getUserInfo(accessToken, openId);
//		System.out.println(JSON.toJSONString(u));
//    	
//		System.out.println(WeChat.message.sendText(accessToken , openId , "测试"));
//    	Map<String, Object> mgs = WeChat.uploadMedia(accessToken, "image", new File("C:\\Users\\lyg\\Desktop\\捕获.PNG"));
//    	System.out.println(JSON.toJSONString(mgs));
//    }
}